﻿using ERPSystem.Common.Domains.SalesOrderCommom;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using MediatR;
using NPOI.OpenXmlFormats.Spreadsheet;
using SqlSugar;
using System.Reflection.Metadata;
using System.Xml.Linq;

namespace SalesManagementCommon.API.SalesManagementCommons.OutOfStock
{
    public class OutOfStockBackHander : IRequestHandler<OutOfStockBackCommand, Result<OutOfStockDTO>>
    {
        private readonly IBaseRepository<SaleOutOfWarehouse>  saleOutOfWarehouseRepository;
        private readonly IBaseRepository<SellOutMaterials> sellOutMaterialsRepository;
        public OutOfStockBackHander(IBaseRepository<SaleOutOfWarehouse> saleOutOfWarehouseRepository, IBaseRepository<SellOutMaterials> sellOutMaterialsRepository)
        {
             this.saleOutOfWarehouseRepository = saleOutOfWarehouseRepository;
             this.sellOutMaterialsRepository = sellOutMaterialsRepository;
         
        }

        /// <summary>
        /// 销售出库反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<OutOfStockDTO>> Handle(OutOfStockBackCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<OutOfStockDTO>();
            try
            {
                var saleOutOfWarehouse = await(await saleOutOfWarehouseRepository.GetAll()).ToListAsync();
                var sellOutMaterials = await(await sellOutMaterialsRepository.GetAll()).ToListAsync();
                var list = (from a in saleOutOfWarehouse
                            where a.DocumentNumber == request.DocumentNumber
                            select new OutOfStockDTO
                            {
                                DocumentNumber = a.DocumentNumber,
                                DocumentDate = a.DocumentDate,
                                DocumentStatus = a.DocumentStatus,
                                DocumentType = a.DocumentType,
                                BusinessType = a.BusinessType,
                                BusinessStatus = a.BusinessStatus,
                                BusinessUnit = a.BusinessUnit,
                                BusinessPersonnel = a.BusinessPersonnel,
                                Period = a.Period,
                                WarehouseId = a.WarehouseId,
                                InvoiceNumber = a.InvoiceNumber,
                                ShippingType = a.ShippingType,
                                ModeDelivery = a.ModeDelivery,
                                CustomerProfileName = a.CustomerProfileName,
                                NoteInformation = a.NoteInformation,
                                OperatorId = a.OperatorId,
                                ItemBianma = a.ItemBianma,
                                SerialNumber = a.SerialNumber,
                                outOfStockWarehouseDTOs = new List<OutOfStockWarehouseDTO>()
                            }).FirstOrDefault();
                if (list != null)
                {
                    list.outOfStockWarehouseDTOs = (from a in sellOutMaterials
                                                    where a.SerialNumber == list.SerialNumber
                                                    select new OutOfStockWarehouseDTO
                                                    {
                                                        MaterialName = a.MaterialName,
                                                        TypeSpecification = a.TypeSpecification,
                                                        OutboundQuantity = a.OutboundQuantity,
                                                        LotNumber = a.LotNumber,
                                                        Remark = a.Remark,
                                                        ItemBianma= a.ItemBianma,
                                                        DocumentNumber = a.DocumentNumber,
                                                        DocumentType = a.DocumentType,
                                                        SerialNumber = a.SerialNumber
                                                    }).ToList();
                        
                }
                result.Data = list;
            }
            catch (Exception ex)
            {

            }
            return result;
        }
    }
}
